home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13422 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  52 lines

  1. Path: indirect.indirect.com!tmarks
  2. From: tmarks@indirect.com (Tom Marks)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Arrays of strings (linking)
  5. Date: Sat, 6 Apr 1996 02:51:31 UNDEFINED
  6. Organization: Internet Direct, Inc.
  7. Message-ID: <tmarks.4.008E8EAF@indirect.com>
  8. References: <4jbkpg$5m7@news.ariadne-t.gr> <315B6DBA.655413B1@alcyone.com>
  9. NNTP-Posting-Host: s81.phxslip4.indirect.com
  10. X-Newsreader: Trumpet for Windows [Version 1.0 Rev B final beta #1]
  11.  
  12. In article <315B6DBA.655413B1@alcyone.com> Erik Max Francis <max@alcyone.com> writes:
  13. >From: Erik Max Francis <max@alcyone.com>
  14. >Subject: Re: Arrays of strings (linking)
  15. >Date: Thu, 28 Mar 1996 20:57:30 -0800
  16.  
  17. >Arnellos Argiris-TEIA wrote:
  18. >> 
  19. >> I have problem writing a function that accepts two strings and then linking
  20. >> them together in a new string. I must also return a pointer to this new
  21. >> string. For example: if I pass "Hello" to string1 and "World" to string2
  22. >> the function must return a pointer to "Hello World!".
  23. >> Could anyone help me by giving me an example or any ideas.
  24.  
  25. >Your use of the word "linking" here is confusing, since it has a very specific
  26. >meaning in C, and is unrelated to the type of operation you're describing
  27. >(which is string concatenation).
  28.  
  29. >Look into the stdlib functions strcpy and strcat.
  30.  
  31. >-- 
  32. char *concat_strings(char *_s1, char *_s2
  33.                                   char *_rs  )
  34. {
  35.  
  36. char *_trs = _rs;
  37.  
  38.  
  39. while(*_s1)
  40.     *_rs++ = *_s1++;
  41.  
  42. while(*_s2)
  43.     *_rs++ = *_s2++;
  44.  
  45. *_rs = '\0';
  46.  
  47. return _trs;
  48. }
  49.  
  50. This code should do what you want. You send it 3 pointers. Pointer to string 1 
  51. pointer to string 2 and a pointer to the resultant string.
  52.